import router from '@/router'
import store from '@/store'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

const whiteList = ['/login', '/404']

router.beforeEach(async(to, from, next) => {
  NProgress.start()
  const token = store.getters.token
  if (token) {
    if (!store.state.user.userInfo.id) {
      await store.dispatch('user/getInfo')
    }
    if (to.path === '/login') {
      next('/')
    } else {
      next()
    }
  } else {
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next({ path: '/login' })
    }
  }
  NProgress.done()
})

router.afterEach((to, from, next) => {
  if (to.meta.title) {
    document.title = '人力资源管理系统-' + to.meta.title
  } else {
    document.title = '人力资源管理系统'
  }
  NProgress.done()
})
